<template>
	<view class="">
		<view class="bg_FFFFFF u-p-20" style="margin-top: 20rpx;">
			<view class="" style="border-bottom: 1rpx solid #F4F4F4; padding:10rpx 0 ;">
				<u-input type='text' placeholder='收货人' v-model='name'></u-input>
			</view>
			<view class="" style="border-bottom: 1rpx solid #F4F4F4; padding:10rpx 0 ;">
				<u-input type='number' maxlength='11' placeholder='手机号' v-model='phone'></u-input>
			</view>

			<view class="u-flex u-row-between" style="border-bottom: 1rpx solid #F4F4F4; padding:30rpx 0 ;"
				@click="show=true">
				<view class="cl_999">{{address}}</view>
				<u-icon name="arrow-right" color='#999999'></u-icon>
			</view>
			<view class="u-flex u-row-between u-w-700" style="border-bottom: 1rpx solid #F4F4F4; ">
				<u-input class="u-w-700" type='areatext' height='120' placeholder='详细地址' v-model='addall'></u-input>
			</view>
		</view>

		<view class="u-m-t-20 u-p-20 bg_FFFFFF u-flex u-row-between">
			<view class="">设为默认地址</view>
			<view class="">
				<u-image src="https://www.sqkjkj.vip/wxImg/common/shop/c1.png" mode="aspectFill" width="30rpx" height='30rpx' v-if='num==1'
					@click='num=0'></u-image>
				<u-image src="https://www.sqkjkj.vip/wxImg/common/shop/c.png" mode="aspectFill" width="30rpx" height='30rpx' v-else
					@click='num=1'></u-image>
			</view>
		</view>

		<view class="u-fixed u-w-750 u-b-0 u-z-300 u-p-25 bs_EFEFEF ">
			<view class="u-h-98 u-l-h-98 u-text-center cl_FFF u-b-r-20 u-w-640 u-auto"
				style="background:linear-gradient(-64deg, #FF382C, #FF633F);" @click="add">
				完成
			</view>
		</view>

		<u-select v-model="show" child-name="child" label-name="area_name" value-name="id" mode="mutil-column-auto" :list="list" @confirm="confirm"></u-select>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				// 收货人
				name: '',
				// 手机号
				phone: '',
				// 邮政编码
				number: "",
				// 地址
				address: '请选择',
				// 详细地址
				addall: '',
				// 设为默认地址
				num: '0',
				// 是否是编辑
				obj: '',
				show: false,
				list: [],
				city_name: '',
				provent: '请选择',
				citys: '',
				area: '',
				nums: 1,
				region: '',
				code: '',
				my_address: ''
			}
		},
		onReady() {

		},
		onLoad(obj) {
			this.obj = obj.num
			// 获取省市区
			this.city()
			if (this.obj == 2) {
				this.my_address = uni.getStorageSync('address')
				this.name = this.my_address.name
				this.phone = this.my_address.phone
				this.addall = this.my_address.addr
				let arr = (this.my_address.modify_region.name).split('/')
				this.code = this.my_address.region
				this.address = arr[0] + arr[1] + arr[2]
				this.num = this.my_address.is_default
			}
		},
		onShow() {

		},
		// 下拉到底事件
		onReachBottom() {

		},
		methods: {
			// 获取省市区
			city() {
				this.$api.get_city({}, res => {
					if (res.data.code == 1) {
						this.list = res.data.data
					}
				})
			},
			confirm(e){
				this.address=`${e[0].label}${e[1].label}${e[2].label}`
				this.code=e[2].value
			},
			// 点击完成
			add() {
				console.log(this.num);
				if (this.name.trim() == '') {
					uni.showToast({
						icon: 'none',
						title: '请填写收货人'
					})
				} else if (this.phone.trim() == '' || !(
						/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/).test(this.phone)) {
					uni.showToast({
						icon: 'none',
						title: "请输入正确手机号码"
					})

				} else if (this.code == '') {
					uni.showToast({
						icon: 'none',
						title: '请选择到区'
					})
				} else if (this.addall.trim() == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入详细地址'
					})
				} else {
					if (this.obj== 1) {
						this.$api.my_address_add({
							name: this.name,
							phone: this.phone,
							region: this.code,
							addr: this.addall,
							is_default: this.num
						}, res => {
							// console.log(res);
							if (res.data.code == 1) {
								uni.showToast({
									icon: 'success',
									title: '添加成功'
								})
								setTimeout(() => {
									uni.navigateBack()
								}, 500)

							} else {
								uni.showToast({
									icon: 'error',
									title: res.data.msg
								})
							}
						})
					} else {
						this.$api.chnage_my_address_add({
							id:this.my_address.id,
							name: this.name,
							phone: this.phone,
							region: this.code,
							addr: this.addall,
							is_default: this.num
						}, res => {
							// console.log(res);
							if(res.data.code==1){
								uni.showToast({
									icon:'success',
									title:'修改成功'
								})
								setTimeout(()=>{
									uni.navigateBack()
								},500)
							}else{
								uni.showToast({
									icon:'error',
									title:res.data.msg
								})
							}
						})
					}

				}
			},
			close() {
				this.region = ''
				this.show = false
				this.num = 1
			},



		}
	}
</script>

<style lang="scss">
	page {
		background: #f5f5f5;
	}

	.one {
		color: red;
		font-weight: 500;
	}
</style>
